package com.yang;

import com.mojang.authlib.GameProfile;
import com.yang.command.CreateNPC;
import com.yang.listener.ListenerNPC;
import net.minecraft.core.BlockPosition;
import net.minecraft.network.protocol.game.ClientboundSetActionBarTextPacket;
import net.minecraft.network.protocol.game.PacketPlayOutNamedEntitySpawn;
import net.minecraft.resources.MinecraftKey;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.EntityPlayer;
import net.minecraft.server.level.PlayerInteractManager;
import net.minecraft.server.level.WorldServer;
import net.minecraft.world.entity.EntityTypes;
import net.minecraft.world.entity.player.EntityHuman;
import net.minecraft.world.level.World;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_20_R1.CraftServer;
import org.bukkit.craftbukkit.v1_20_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_20_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.Optional;
import java.util.UUID;

public final class CitizensNPC extends JavaPlugin {

    @Override
    public void onEnable() {
        this.saveDefaultConfig();
        onListener();
        onCommand();
    }
    public void spawnNPC(Player player) {
        try {
            // 获取玩家所在的世界
            CraftWorld craftWorld = (CraftWorld) player.getWorld();

            // 创建自定义 NPC 实体
            EntityPlayer customNPC = createCustomNPC(craftWorld, player);

            // 将 NPC 添加到世界中
            craftWorld.getHandle().a(customNPC);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private EntityPlayer createCustomNPC(CraftWorld craftWorld, Player player) {
        // 获取玩家实体的位置和方向
        Location playerLocation = player.getLocation();
//        double playerX = playerLocation.getX();
//        double playerY = playerLocation.getY();
//        double playerZ = playerLocation.getZ();
//        float playerYaw = playerLocation.getYaw();
//        float playerPitch = playerLocation.getPitch();

        // 创建 NPC 实体并设置其位置和方向
        EntityPlayer npc = new EntityPlayer(((CraftServer) Bukkit.getServer()).getServer(), craftWorld.getHandle(),null);
        npc.setPosRaw(0,100,0,true);
npc.visibleByDefault = true;
        return npc;
    }
    // 加载指令
    private void onCommand() {
        this.getServer().getPluginCommand("createNpc").setExecutor(new CreateNPC(this));
    }

    // 加载监听
    private void onListener() {
        this.getServer().getPluginManager().registerEvents(new ListenerNPC(this), this);
    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
    }
}
